import { resolveToken } from '../utils';

const token: { [key: string]: string } = {
  admin: 'admin',
  editor: 'editor',
};

export interface AuthApiResponse {
  code: number;
  data?: {
    token: string;
  };
  message?: string;
}

const authApiRoutes: {
  url: string;
  method: string;
  response: ({ body, headers }: { body?: any; headers?: any }) => AuthApiResponse;
}[] = [
  {
    url: '/api/auth/login',
    method: 'post',
    response: ({ body }) => {
      if (['admin', 'editor'].includes(body?.name)) {
        return {
          code: 0,
          data: {
            token: token[body.name],
          },
        };
      } else {
        return {
          code: -1,
          message: '没有此用户',
        };
      }
    },
  },
  {
    url: '/api/auth/refreshToken',
    method: 'post',
    response: ({ headers }) => {
      return {
        code: 0,
        data: {
          token: resolveToken(headers?.authorization),
        },
      };
    },
  },
];

export default authApiRoutes;